package haxe.util.benchmark; 
import haxe.util.Stack;

class BenchmarkStack extends BenchmarkHelper, implements Benchmark
{
	
	var s:haxe.util.Stack<TestData>;
	
	public function new( iterations:Int ) 
	{
		super( iterations );
		s = new Stack<TestData>();
	}
	
	private function runAdd():Void
	{
		s = new Stack<TestData>();
		this.init( "Stack.push["+iterations+"]" );
		for ( i in 0...iterations )
			s.push(new TestData("test"+i,i) );
		this.done();
	}
	
	private function runToArray():Void
	{
		s = new Stack<TestData>();
		for ( i in 0...iterations )
			s.add( new TestData("test"+i,i) );
		this.init( "Stack.toArray["+iterations+"]" );
		var a = s.toArray();
		this.done();
	}
	
	private function runIterate():Void
	{
		s = new Stack<TestData>();
		for ( i in 0...iterations )
			s.add( new TestData("test"+i,i) );
		this.init( "Stack.iterate["+iterations+"]" );
		for ( i in s ){i;}
		this.done();
	}
	
	private function runRemove():Void
	{
		s = new Stack<TestData>();
		var a:Array<TestData> = new Array<TestData>();
		var rand:Float;
		for ( i in 0...iterations )
		{
			rand = Math.random();
			s.add( new TestData("test"+rand, rand) );
			if ( i % 5 == 0 )
				a.push(  new TestData("test"+rand, rand) );
		}
		this.init( "Stack.remove["+(iterations/5)+" from "+iterations+"]" );
		for ( i in a )
		{
			s.remove( i );
		}
		this.done();
	}
	
	private function runRemoveWithFunction():Void
	{
		s = new Stack<TestData>();
		var a:Array<TestData> = new Array<TestData>();
		var rand:Float;
		for ( i in 0...iterations )
		{
			rand = Math.random();
			s.add(  new TestData("test"+rand, rand) );
			if ( i % 5 == 0 )
				a.push(  new TestData("test"+rand, rand) );
		}
		//var f = function(a:TestData,b:Float):Bool{return a == b; };
		this.init( "Stack.remove( with function )["+(iterations/5)+" from "+iterations+"]" );
		for ( i in a )
		{
			s.remove( i, TestData.equals );
		}
		this.done();
	}
	
	private function runPop():Void
	{
		s = new Stack<TestData>();
		for ( i in 0...iterations )
			s.add(  new TestData("test"+i, i) );
		init("Stack.pop["+iterations+"]");
		while( !s.isEmpty() )
			s.pop();
		done();
	}
	
	private function runContains():Void
	{
		s = new Stack<TestData>();
		var a:Array<TestData> = new Array<TestData>();
		var rand:Float;
		for ( i in 0...iterations )
		{
			rand = Math.random();
			s.add(  new TestData("test"+rand, rand) );
			if ( i % 10 == 0 )
				a.push(  new TestData("test"+rand, rand) );
		}
		this.init( "Stack.contains["+(iterations/10)+" from "+iterations+"]" );
		for ( i in a )
		{
			s.contains( i );
		}
		this.done();
	}
}